﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Pibby.Core;


namespace Pibby.Core.Tests
{
	[TestClass]
	public class PodcastCollectionTests
	{
		private int ChangeCounter;

		[TestInitialize]
		public void Setup()
		{
			this.ChangeCounter = 0;
		}

		[TestMethod]
		public void CannotIndexEmptyCollection()
		{
			PodcastCollection collect = new PodcastCollection();

			Podcast missing = collect["not expecting to find anything"];

			Assert.IsNull(missing);
		}

		[TestMethod]
		public void CannotFindMissingItem()
		{
			PodcastCollection collect = new PodcastCollection();

			Uri uri = new Uri("http://p1.com/");
			Podcast p = new Podcast(uri);

			collect.Add(p);

			Podcast missing = collect["not expecting to find this"];

			Assert.IsNull(missing);
		}

		[TestMethod]
		public void CanContainSingleItem()
		{
			PodcastCollection collect = new PodcastCollection();

			Uri uri = new Uri("http://p1.com/");
			Podcast p = new Podcast(uri);

			collect.Add(p);

			Assert.AreEqual(1, collect.Count);
			Assert.IsTrue(collect.Contains(p));
		}

		[TestMethod]
		public void CanIndexSingleItemByUri()
		{
			PodcastCollection collect = new PodcastCollection();

			Uri uri = new Uri("http://p1.com/");
			Podcast p = new Podcast(uri);

			collect.Add(p);

			Podcast p2 = collect[p.Id];
			Assert.AreEqual(p, p2);
		}

		[TestMethod]
		public void CanIndexByUriInMultiItemCollection()
		{
			PodcastCollection collect = new PodcastCollection();

			Uri uri = new Uri("http://p0.com/");
			Podcast p = new Podcast(uri);

			for (int i = 1; i < 100; ++i)
			{
				if (i == 99)
				{
					// sneak the one we want to find here...
					collect.Add(p);
				}
				else
				{
					Uri loopUri = new Uri(string.Format("http://p{0}.com/", i));
					collect.Add(new Podcast(loopUri));
				}
			}

			Podcast p2 = collect[p.Id];
			Assert.AreEqual(p, p2);
		}

		[TestMethod]
		public void CanIndexSingleItemByGuid()
		{
			PodcastCollection collect = new PodcastCollection();

			Uri uri = new Uri("http://p1.com/");
			Podcast p = new Podcast(uri);

			collect.Add(p);

			Podcast p2 = collect[p.Id];
			Assert.AreEqual(p, p2);
		}

		[TestMethod]
		public void CanIndexByGuidMultiItemCollection()
		{
			PodcastCollection collect = new PodcastCollection();

			Uri uri = new Uri("http://p0.com/");
			Podcast p = new Podcast(uri);

			for (int i = 1; i < 100; ++i)
			{
				if (i == 50)
				{
					// sneak the one we want to find here...
					collect.Add(p);
				}
				else
				{
					Uri loopUri = new Uri(string.Format("http://p{0}.com/", i));
					collect.Add(new Podcast(loopUri));
				}
			}

			Podcast p2 = collect[p.Id];
			Assert.AreEqual(p, p2);
		}

		[TestMethod]
		public void SortingCollectionSortsByDescendingScore()
		{
			PodcastCollection collect = new PodcastCollection();

			for (int i = 1; i < 100; ++i)
			{
				Uri loopUri = new Uri(string.Format("http://p{0}.com/", i));
				collect.Add(new Podcast(loopUri)
					{
						Score = i
					});
			}

			Assert.AreEqual(1, collect[0].Score);
			Assert.AreEqual(99, collect[98].Score);

			collect.Sort();

			Assert.AreEqual(99, collect[0].Score);
			Assert.AreEqual(1, collect[98].Score);
		}
	}
}
